/*
AlliedBridge Content Management System
Copyright (C) 2006 AlliedBridge

This program is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software Foundation;

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.volume4.alliedbridge.model.media;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.io.*;
import java.util.*;
import java.net.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.commons.fileupload.*;
import org.volume4.utils.*;

/**
 *
 * @author  Design_DEMON
 * @version 1.0
 * Based on base classes from org.apache.commons.fileupload
 */
public class UploadFile extends HttpServlet {
    public String folder = "/Program Files/Apache Group/Tomcat 4.1/webapps/magforcetrading/cataloque/";
    public String tempFolder = "/temp";    
    //public String folder = "/var/www/vhosts/Magforce/webapps/Magforce/cataloque/";    
    //public String tempFolder = "/var/www/vhosts/Magforce/webapps/Magforce/temp/";    
    public String DRIVER, URL, USER, PASS, fieldName, fieldData, fileName, fileFieldName;
    public String prodName, prodDetail, prodImgURI, prodImgPreview; 
        
    public void init() throws ServletException {
    ServletContext context = getServletContext();
    DRIVER = context.getInitParameter("driver");
    URL = context.getInitParameter("dburl");
    USER = context.getInitParameter("dbuser");
    PASS = context.getInitParameter("dbpass");
    }
    
    protected void processRequest(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, java.io.IOException {        
        
        res.setContentType("text/html");
        PrintWriter out = res.getWriter();
        Connection con = null;
        
        try {            
          
        	// Create a new file upload handler
            DiskFileUpload upload = new DiskFileUpload();

            // Set upload parameters   
            upload.setSizeMax(-1);
            upload.setSizeThreshold(4096);
            upload.setRepositoryPath(tempFolder);

            // Parse the request
            List items = upload.parseRequest(req);

            // Process the uploaded fields
            Iterator iter = items.iterator();
            while (iter.hasNext()) {
            FileItem item = (FileItem) iter.next();        

            if (item.isFormField()) {               
                if(item.getFieldName().equals("job")) {
                    prodName = ServletUtilities.filter(item.getString());                    
                } 
            } else {
                if(item.getFieldName().equals("uploadPDF")) {
                
                int i = item.getName().lastIndexOf('\\');
                prodImgURI = ServletUtilities.filter(item.getName().substring(i+1));
                File savedFile = new File(folder, ServletUtilities.filter(item.getName().substring(i+1)) ); 
                item.write(savedFile);                
                }   
                    
            	}
            }

res.sendRedirect("../../result/product_added.jsp");
            
        }   catch(FileUploadException fue) {         
         fue.printStackTrace();
         out.println("There was and error when reading and writing the file to the server.");
               
        } catch(Exception e) {
            e.printStackTrace();
        }
}
    
    protected void doGet(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, java.io.IOException {
        processRequest(req, res);
    }    
    
    protected void doPost(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, java.io.IOException {
        processRequest(req, res);
    }
}
